﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Youz.Common.Helper;
using Youz.CreditRepay.BLL;
using Youz.CreditRepay.Entity;

namespace Youz.CreditRepay.Server.PayEase
{
    public partial class Pay : System.Web.UI.Page
    {
        Pay_PaymentTypeBLL PaymentTypeBLL = new Pay_PaymentTypeBLL();
        Pay_PaymentType paymentType = new Pay_PaymentType();
        Pay_RecordsBLL payRecordsBLL = new Pay_RecordsBLL();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var account = WebHelper.Get("account");
                var tradeNo = WebHelper.Get("tradeNo");
                var pmode = WebHelper.Get("mode");
                var payRecords = payRecordsBLL.Get(tradeNo);

                if (payRecords != null)
                {
                    payRecordsBLL.UpdateStatus(payRecords.TradeNo, PayRecordsStatus.Paying);

                    paymentType = PaymentTypeBLL.Get(payRecords.PayType.ToString()); //支付方式信息

                    this.v_mid.Text = paymentType.MerchantNo; //商户编号,公司商号8738，国际8739（跨境）
                    this.v_oid.Text = GetOrderNo(paymentType.MerchantNo, payRecords.TradeNo);//订单编号 hms可以用自己的订单编号，但是要保证每一次提交订单编号是唯一值。订单编号的格式不能变
                    this.v_rcvname.Text = HttpUtility.UrlEncode(account);//"8738";//收货人姓名order.Username
                    this.v_rcvaddr.Text = paymentType.MerchantNo;//  //收货人地址8738
                    this.v_rcvtel.Text = "00000000"; //收货人电话00000000
                    this.v_rcvpost.Text = paymentType.MerchantNo;  //收货人邮编8738
                    this.v_amount.Text = payRecords.Amount.ToString("f2"); //订单总金额
                    this.v_ymd.Text = DateTime.Now.ToString("yyyyMMdd");   //订单产生的日期
                    this.v_orderstatus.Text = "1"; //商户配货状态1
                    this.v_ordername.Text = paymentType.MerchantNo; //订货人姓名8738
                    this.v_moneytype.Text = "0"; //币种 0，人民币；1，美元
                    this.v_url.Text = paymentType.Getway; //支付完成后的实时返回地址。http://localhost:27301/Order/GetOrdersInfo.aspx
                    this.v_producttype.Text = "00";//产品类型00
                    this.v_idtype.Text = "00";//证件类型00
                    this.v_idnumber.Text = "000000000000000";//客户证件号码000000000000000
                    this.v_idname.Text = HttpUtility.UrlEncode(account);// "none";//客户姓名order.ShipTo
                    this.v_idcountry.Text = "000";//客户国籍000
                    this.v_idaddress.Text = "none";//客户联系地址none
                    this.v_userref.Text = HttpUtility.UrlEncode(account);//客户唯一标识可以为邮箱，学号等等  可以唯一标识身份信息  
                    this.v_pmode.Text = pmode;
                    
                    ClassHmac2003.ClassHmac cc = new ClassHmac2003.ClassHmac();
                    string try_try = this.v_moneytype.Text + this.v_ymd.Text + this.v_amount.Text + this.v_rcvname.Text + this.v_oid.Text + this.v_mid.Text + this.v_url.Text;
                    this.v_md5info.Text = cc.hex_hmac_md5(try_try, paymentType.MerchantKey);////test，是测试商户号配套密钥。接口调试成功后，可以自定义一个16位的数字字母组合作为密钥，然后发邮件到kf@payeasenet.com.
                }
                else
                {
                    Response.End();
                }
            }
        }

        /// <summary>
        /// 获得支付订单ID，格式为：日期-商户编号-商户订单号
        /// </summary>
        /// <param name="MerchantNo"></param>
        /// <param name="no"></param>
        /// <returns></returns>
        private string GetOrderNo(string MerchantNo, string no)
        {
            return string.Format("{0}-{1}-{2}", DateTime.Today.ToString("yyyyMMdd"), MerchantNo, no);
        }
    }
}